package com.ssm.controller;

import com.ssm.pojo.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;

/**
 * Author  zxy
 * Date  2022/11/16 上午 11:31
 */
@Controller
public class TestParamController {

    /*
    * 通过servletAPT获取请求参数  HttpServletRequest
    * */
    @RequestMapping("/param/servletAPI")
    public String getParamByServletAPI(HttpServletRequest request){
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        System.out.println("username: " + username + ", password: " +password);
        return "success";
    }

    /*
    * 通过控制器方法中的形参获取请求参数
    * @RequestParam(value="", required=  , defaultValue="")
    *
    * @RequestHeader: 将请求头与控制器方法形参形成映射关系
    * @CookieValue：将cookie数据与控制器方法形参形成映射
    *  */
    @RequestMapping("/param")
    public String getParam(
            @RequestParam("username") String username,
            @RequestParam(required = false, defaultValue = "zxy1111") String password){
        System.out.println("username: " + username + ", password: " +password);
        return "success";
    }

    /*
    * 通过控制器方法的形参位置设置实体类型的形参获取请求参数
    * 注：需保证 实体类型中的属性名和请求参数的名字一致
    * */
    @RequestMapping("/param/pojo")
    public String getParamByPojo(User user) {
        System.out.println(user);
        return "success";
    }

}
